/**
  * 系统管理--用户管理的单例对象
 */	
var Article = {
    id: "ArticleTable",//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    deptid:0
};

/**
 * 初始化表格的列
 */
Article.initColumn = function () {
    var columns = [
        {field: 'selectItem', checkbox: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'title', field: 'title', align: 'center', valign: 'middle', sortable: true},
        {
        	title: 'type', 
        	field: 'type',
        	align: 'center',
        	valign: 'middle',
        	sortable: true,
        	formatter : function(value, row,index) {
				if (value == "0") {
					return '通知';
				} else if (value == "1") {
					return '新闻';
				} else if (value == "2") {
					return 'banner新闻';
				}
				else if (value == "3") {
					return "栏目";
				}
			}
        },
        {title: 'createUser', field: 'createUser', align: 'center', valign: 'middle', sortable: true},
        {title: 'createTime', 
        	field: 'createTime',
        	align: 'center',
        	valign: 'middle',
        	sortable: true,
        	formatter:function(value,row,index){
        		if(value){
        			return value.substring(0,19)
        		}
        		return "-";
        	}
        },
        {title: 'updateUser', field: 'updateUser', align: 'center', valign: 'middle', sortable: true},
        {title: 'updateTime', 
        	field: 'updateTime',
        	align: 'center',
        	valign: 'middle',
        	sortable: true,
        	formatter:function(value,row,index){
        		if(value){
        			return value.substring(0,19)
        		}
        		return "-";
        	}
        }
        ];
    return columns;
};

/**
 * 检查是否选中
 */
Article.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Article.seItem = selected;
        return true;
    }
};

Article.singleCheck = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
    	if (selected.length >1) {
            Feng.info("请先选中表格中的某一记录！");
            return false;
        }else{
        	 Article.seItem = selected[0];
             return true;
        } 
    }
};

/**
 * 点击添加管理员
 */
Article.addArticle = function () {
    var index = layer.open({
        type: 2,
        title: '添加用户',
        area: ['100%', '100%'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/article/add'
    });
    this.layerIndex = index;
};

/**
 * 点击修改按钮时
 * @param ArticleId 管理员id
 */		
Article.edit = function () {
    if (this.singleCheck()) {
        var index = layer.open({
            type: 2,
            title: '编辑用户',
            area: ['100%', '100%'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/article/edit/' + this.seItem.id
        });
        this.layerIndex = index;
    }
};
Article.detail = function(){
	if (this.singleCheck()) {
        var index = layer.open({
            type: 2,
            title: '部件属性详情',
            area: ['100%', '100%'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/article/detail/' + this.seItem.id
        });
        this.layerIndex = index;
    }
}


/**
 * 删除用户
 */
Article.delArticle = function () {
	 if (this.singleCheck()) {

	    	
	        var operation = function(){
	            var ajax = new $ax(Feng.ctxPath + "/article/save/delete/" + Article.seItem.id, function (data) {
	            	if(data.resultCode==0){
	            		Feng.success("删除成功!");
	            		Article.table.refresh();
	            	}else{
	            		Feng.error(data.msg + "!");
	            	}
	                
	            }, function (data) {
	                Feng.error("删除失败!" + data.responseJSON.message + "!");
	            });
	            ajax.start();
	        };

	        Feng.confirm("是否刪除选中的字典?", operation);
	    }
};


Article.search = function () {
	Article.table.destroy();
	
	var defaultColunms = Article.initColumn();
    var table = new BSTable("ArticleTable", "/article/search/"+$("#keyword").val(), defaultColunms);
    table.setPaginationType("server");
    Article.table = table.init();
	
};





Article.resetSearch = function () {
    $("#name").val("");
    $("#beginTime").val("");
    $("#endTime").val("");

    Article.search();
}



$(function () {
	Article.table=null;
    var defaultColunms = Article.initColumn();
    var table = new BSTable("ArticleTable", "/article/list", defaultColunms);
    table.setPaginationType("server");
    Article.table = table.init();
});
